﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XclusiveService.Models;

namespace XclusiveService.Controllers
{
    public class ClientController : ApiController
    {
        static readonly IClientRepository repository = new ClientRepository();

        //Getting all the Clients
        public IEnumerable<Client> GetAllClient()
        {
            return repository.GetAllClient();
        }

        //Getting a Client by company_name
        public Client GetClientByCompanyName(string company_name)
        {
            Client c = repository.GetClientByCompanyName(company_name);

            if (c == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return c;
        }

        //Creating a Client (POST)
        public HttpResponseMessage CreateClient(Client c)
        {
            c = repository.CreateClient(c);
            var response = Request.CreateResponse<Client>(HttpStatusCode.Created, c);

            string uri = Url.Link("DefaultApi", new { username = c.username });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        //Update a Client (PUT)
        public void UpdateClient(string company_name, Client c)
        {
            c.company_name = company_name;

            if (!repository.UpdateClient(c))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        //Deleting a Client
        public HttpResponseMessage DeleteClient(string company_name)
        {
            repository.DeleteClient(company_name);

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
